package controllers;

import models.User;
import play.mvc.Http.Context;
import play.mvc.Result;
import play.mvc.Security;
import utils.Key;

public class Secured extends Security.Authenticator {
    
    @Override
    public String getUsername(Context ctx) {
        String userId = ctx.session().get(Key.USER_ID);
        if (userId != null) {
	        User user = User.getById(Long.parseLong(userId));
	        if (user != null) {
	        	return user.email;
	        }
        }
        return null;
    }
    
    @Override
    public Result onUnauthorized(Context ctx) {
        return redirect(routes.Application.login());
    }

    
}